/*
 * 版权所有 (c) 2022-2099 TBIT。保留所有权利。
 */

package com.tbit.uqbike.service.business.impl;

import cn.hutool.core.collection.CollUtil;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.DetentionArea;
import com.tbit.uqbike.object.pojo.Machine;
import com.tbit.uqbike.object.pojo.vo.DetentionMachineBoundVO;
import com.tbit.uqbike.service.base.MachineService;
import com.tbit.uqbike.service.business.DetentionMachineBoundService;
import com.tbit.uqbike.webmanager.dao.core.DetentionAreaDao;
import com.tbit.uqbike.webmanager.dao.core.DetentionMachineBoundMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 扣押设备绑定 服务实现类
 * </p>
 *
 * @author chen
 * @since 2023-02-08
 */
@Service
public class DetentionMachineBoundServiceImpl implements DetentionMachineBoundService {

    @Autowired
    private DetentionMachineBoundMapper detentionMachineBoundMapper;
    @Autowired
    private MachineService machineService;
    @Autowired
    private DetentionAreaDao detentionAreaDao;

    @Override
    public List<DetentionMachineBoundVO> selectByDetentionId(Integer detentionId, Integer accountId) {
        return detentionMachineBoundMapper.selectByDetentionId(detentionId, accountId);
    }

    @Override
    public List<DetentionMachineBoundVO> queryPage(Integer detentionId, Integer accountId, String userCode) {
        return detentionMachineBoundMapper.queryPage(detentionId, accountId, userCode);
    }

    @Override
    public List<DetentionMachineBoundVO> selectUserCodeList(Integer accountId, List<String> userCodeList) {
        return detentionMachineBoundMapper.selectByAccountIdAndUserCodeList(accountId, userCodeList);
    }

    @Override
    public List<DetentionMachineBoundVO> selectUserCodeList(Integer accountId, Integer detentionId, List<String> userCodeList) {
        return detentionMachineBoundMapper.selectByDetentionIdAndUserCodeList(accountId, detentionId, userCodeList);
    }

    @Override
    public void deleteByDetentionIdAndMachineId(Integer accountId, Integer detentionId, Integer machineId) {
        detentionMachineBoundMapper.deleteByDetentionIdAndMachineId(accountId, detentionId, machineId);
    }

    @Override
    public int batchAddDetentionMachine(Integer accountId, Integer detentionId, List<String> userCodeList, Date addTime,
                                        Integer userId) {
        List<String> distinctUserCodeList = userCodeList.stream()
                .distinct()
                .collect(Collectors.toList());
        List<Machine> dbMachineList = machineService.getByBrandIdAndAccountId(null, accountId);
        List<String> dbUserCodeList = dbMachineList.stream().map(Machine::getUserCode).collect(Collectors.toList());
        List<String> subtract = CollUtil.subtractToList(distinctUserCodeList, dbUserCodeList);
        if (!CollectionUtils.isEmpty(subtract)) {
            throw new BaseException(subtract + "车辆编号不存在");
        }
        List<DetentionMachineBoundVO> detentionMachineBoundList = selectUserCodeList(accountId, distinctUserCodeList);
        if (!CollectionUtils.isEmpty(detentionMachineBoundList)) {
            throw new BaseException(detentionMachineBoundList.get(0).getUserCode() + "车辆已存在于["
                    + detentionMachineBoundList.get(0).getDetentionName() + "]扣押区");
        }
        List<Machine> machineList = machineService.getByUserCodes(distinctUserCodeList);
        QueryWrapper<DetentionArea> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(DetentionArea::getDetentionId, detentionId);
        DetentionArea detentionArea = detentionAreaDao.selectOne(wrapper);
        return detentionMachineBoundMapper.batchAddDetentionMachine(accountId, detentionId, machineList, userId, addTime,
                detentionArea.getName());
    }

    @Override
    public int batchDeleteDetentionMachine(Integer accountId, Integer detentionId, List<String> userCodeList) {
        List<Machine> dbMachineList = machineService.getByBrandIdAndAccountId(null, accountId);
        List<String> dbUserCodeList = dbMachineList.stream().map(Machine::getUserCode).collect(Collectors.toList());
        Set<String> userCodeSet = CollUtil.intersectionDistinct(userCodeList, dbUserCodeList);
        List<String> distinctUserCodeList = new ArrayList<>(userCodeSet);
        List<DetentionMachineBoundVO> detentionMachineBoundList = selectUserCodeList(accountId, detentionId, distinctUserCodeList);
        if (CollectionUtils.isEmpty(detentionMachineBoundList)){
            return 0;
        }
        return detentionMachineBoundMapper.deleteBatchIds(detentionMachineBoundList.stream()
                .map(DetentionMachineBoundVO::getBoundId).collect(Collectors.toList()));
    }
}
